#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./appengine/api/http.py
# Compiled at: 2017-11-16 15:44:28
"""
Tencent is pleased to support the open source community by making 蓝鲸智云(BlueKing) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.

请求app engine的底层方法

Rules:
1. POST/DELETE/PUT: json in - json out, 如果resp.json报错, 则是engine接口问题
2. GET带参数 HEAD不带参数
3. 以统一的header头发送请求

Copyright © 2012-2017 Tencent BlueKing. All Rights Reserved. 蓝鲸智云 版权所有
"""
import requests, logging

def _gen_header(sid, token):
    headers = {'Content-Type': 'application/json',
       'X-ID': sid,
       'X-TOKEN': token
       }
    return headers


def _http_request(method, url, headers=None, data=None):
    try:
        if method == 'GET':
            resp = requests.get(url=url, headers=headers, params=data)
        else:
            if method == 'HEAD':
                resp = requests.head(url=url, headers=headers)
            else:
                if method == 'POST':
                    resp = requests.post(url=url, headers=headers, json=data)
                else:
                    if method == 'DELETE':
                        resp = requests.delete(url=url, headers=headers, json=data)
                    else:
                        if method == 'PUT':
                            resp = requests.put(url=url, headers=headers, json=data)
                        else:
                            return (
                             False, None)
    except requests.exceptions.RequestException:
        logging.exception('engine http request error! type: %s, url: %s, data: %s' % (method, url, str(data)))
        return (
         False, None)

    if resp.status_code != 200:
        if resp.status_code == 403:
            return (True, resp.json())
        logging.error('engine http request error! type: %s, url: %s, data: %s, response_status_code: %s, response_content: %s' % (
         method, url, str(data), resp.status_code, resp.content))
        return (
         False, None)
    return (
     True, resp.json())
    return


def http_get(url, sid, token, params):
    headers = _gen_header(sid, token)
    return _http_request(method='GET', url=url, headers=headers, data=params)


def http_post(url, sid, token, params):
    headers = _gen_header(sid, token)
    return _http_request(method='POST', url=url, headers=headers, data=params)
